<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <p>rootCount:{{rootCount}}</p>
    <p>count1:{{count1}}</p>
    <p>doubleCount1:{{doubleCount1}}</p>
    <p>count2:{{count2}}</p>
    <p>doubleCount2:{{doubleCount2}}</p>
    <p>addRootCount:{{addRootCount}}</p>
    <button @click="add">提交add的commit</button>
    <button @click="increment">派发increment的action</button>
    <button @click="incrementByRootCount">派发incrementByRootCount的action</button>
  </div>
  <script src="vue.js"></script>
  <script src="my-vuex.js"></script>
  <script>
    //Vue中使用Vuex插件，此插件的作用是给Vue实例添加$store属性
    Vue.use(Vuex);
    let counter1 = {
      state: { count: 1 },
      mutations:{
        add(state,payload=1,rootState){
          state.count+=payload;
        }
      },
      getters: {
        //在模块内的getters中state指的是模块内的局部状态
        //对于模块内部的 getter，根节点状态会作为第三个参数暴露出来
        doubleCount1(state, getters, rootState) {
          return state.count * 2;
        },
        addRootCount(state, getters, rootState){
          return state.count+rootState.count;
        }
      },
      actions:{
        increment({commit,state,rootState}){
          commit('add');
        },
        incrementByRootCount({commit,state,rootState}){
          //在提交add变更的时候，payload传递的是全局命名空间下的count
          commit('add',rootState.count);
        }
      }
    }
    let counter2 = {
      state: { count: 2 },
      mutations:{
        add(state,payload,rootState){
          state.count++;
        }
      },
      getters: {
        doubleCount2(state, getters, rootState) {
          return state.count * 2;
        }
      },
      actions:{
        increment({commit}){
          commit('add');
        }
      }
    }
    const store = new Vuex.Store({
      state: { count: 10 },
      mutations:{
        add(state,payload){
          state.count++;
        }
      },
      actions:{
        increment({commit}){
          commit('add');
        }
      },
      modules: {
        counter1,
        counter2
      }
    })
    var vm = new Vue({
      el: '#app',
      store,
      computed: {
        rootCount() {
          return this.$store.state.count;
        },
        count1() {
          return this.$store.state.counter1.count;
        },
        count2() {
          return this.$store.state.counter2.count;
        },
        doubleCount1() {
          return this.$store.getters.doubleCount1;
        },
        addRootCount() {
          return this.$store.getters.addRootCount;
        },
        doubleCount2() {
          return this.$store.getters.doubleCount2;
        }
      },
      methods:{
        add(){
          this.$store.commit('add');
        },
        increment(){
          this.$store.dispatch('increment');
        },
        incrementByRootCount(){
          this.$store.dispatch('incrementByRootCount')
        }
      }
    })

  </script>
</body>

</html>